package jdk8的特性.Stream.流的基本操作;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static jdk8的特性.Stream.流的基本操作.MockDB.mockUserList;

/**
 * @author idea
 * @data 2018/9/8
 */
public class Test {
    public static void main(String[] args) {
        List<User> userList = mockUserList(10);
        //使用申明式的流
        List<String> usernameList = userList.stream()
                .filter((user) -> user.getSex() == 1 && user.getAge() == 12)
                .sorted(Comparator.comparing(User::getId))
                .map(User::getUsername)
                .limit(1)
                .collect(Collectors.toList());
        for (String username : usernameList) {
            System.out.println(username);
        }


        //使用多核架构来执行代码
        List<String> usernames=userList.parallelStream()
                .filter((user)->user.getSex()==0 && user.getAge()==10)
                .sorted(Comparator.comparing(User::getId))
                .map(User::getUsername)
                .limit(1)
                .collect(Collectors.toList());


        for (String username : usernames) {
            System.out.println(username);
        }


        //多条件进行流的筛选
        List<User> newUserList=userList.parallelStream()
                .filter((user)-> user.getSex()==1 &&user.getAge()==12)
                .collect(Collectors.toList());
        for (User user : newUserList) {
            System.out.println(user);
        }


    }
}
